package com.zyg.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyg.pojo.EchartsData;
import com.zyg.pojo.Hostel;
import com.zyg.service.HostelService;
import com.zyg.util.LayData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/hostel")
public class HostelController {

    @Autowired
    private HostelService hostelService;

    @RequestMapping("/tolist")
    public  String tolist(){
        return "hostel/list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public String list(Hostel hostel, @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "10") Integer limit){
        // 查询数据
        LayData layData = new LayData();

        QueryWrapper<Hostel> qw = new QueryWrapper<>();

        qw.like(hostel.getHostelName()!=null && !"".equals(hostel.getHostelName()),"hostel_name",hostel.getHostelName());

        long count = hostelService.count(qw);
        layData.setCount((int) count);
        IPage<Hostel> page1 = new Page<>(page, limit);
        List<Hostel> list = hostelService.list(page1, qw);
        layData.setData(list);


        layData.setCode(0);
        layData.setMsg("查询成功");
        // 数据返回
        String data = JSON.toJSONString(layData);

        return data;
    }

    @RequestMapping("/toadd")
    public String toAdd(){
        return "hostel/add";
    }

    @RequestMapping("/addSave")
    @ResponseBody
    public String addSave(Hostel hostel){
        boolean flag = hostelService.save(hostel);
        if (flag) {
            return JSON.toJSONString(new LayData(0, "添加成功"));
        } else {
            return JSON.toJSONString(new LayData(500, "添加失败"));
        }
    }

    @RequestMapping("/toupdate/{id}")
    public String toUpdate(@PathVariable("id") Integer id,Model model){
        Hostel hostel =hostelService.getById(id);
        model.addAttribute("hostel",hostel);
        return "hostel/update";
    }
    @RequestMapping("/updateSave")
    @ResponseBody
    public String updateSave(Hostel hostel){
        boolean flag = hostelService.updateById(hostel);
        if (flag) {
            return JSON.toJSONString(new LayData(0, "更新成功"));
        } else {
            return JSON.toJSONString(new LayData(500, "更新失败"));
        }
    }

    @RequestMapping("/delete/{id}")
    @ResponseBody
    public String deleteHostel(@PathVariable("id") Integer id){
        boolean flag = hostelService.removeById(id);
        if (flag) {
            return JSON.toJSONString(new LayData(0, "删除成功"));
        } else {
            return JSON.toJSONString(new LayData(500, "删除失败"));
        }
    }
    @RequestMapping("/getData")
    @ResponseBody
    public String getData2(){
        List<EchartsData> data = hostelService.getGradeIndexData();
        Map<String, Object> map = new HashMap<>();
        if (data != null && data.size() > 0){
            map.put("name",data.stream().map(e -> e.getName()).toList());
            map.put("value",data.stream().map(e -> e.getValue()).toList());
        }
        return JSON.toJSONString(map);

    }
}
